package com.meilai.project.service.workflow.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.entity.workflow.WorkFlowDefault;
import com.meilai.project.exception.CommonException;
import com.meilai.project.mapper.workflow.WorkFlowDefaultMapper;
import com.meilai.project.service.workflow.WorkFlowDefaultService;
import com.meilai.project.vo.workflow.WorkFlowDefaultListVO;
import com.meilai.project.vo.workflow.WorkFlowTypeNameAndRouterVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 张驰
 * @date 2020/5/29 15:44
 */
@Service
public class WorkFlowDefaultServiceImpl extends ServiceImpl<WorkFlowDefaultMapper, WorkFlowDefault> implements WorkFlowDefaultService {

    @Override
    public boolean checkStatus(Integer flow_type) {
        return count(new QueryWrapper<WorkFlowDefault>().isNull("deleted_at")
                .eq("status",1).eq("flow_type",flow_type)) > 0;
    }

    @Override
    @Transactional
    public boolean changeStatus(Integer id, Integer flow_type, Integer status) {
        boolean flag = true;
        if(status == 1) flag = update(new UpdateWrapper<WorkFlowDefault>().set("status",0).isNull("deleted_at").eq("flow_type",flow_type));
        return flag && update(new UpdateWrapper<WorkFlowDefault>().set("status",status).eq("id",id));
    }

    @Override
    public boolean delProcess(Long id) {
        return update(new UpdateWrapper<WorkFlowDefault>().set("deleted_at", LocalDateTime.now()).eq("id",id));
    }

    @Override
    public Page<WorkFlowDefaultListVO> selectPageList(Integer current_page, Integer page_size, String name, Integer status, Integer flow_type) {
        Page<WorkFlowDefaultListVO> page = new Page<WorkFlowDefaultListVO>(current_page,page_size);
        List<WorkFlowDefaultListVO> list = baseMapper.selectPageList(page,name,status,flow_type);
        return page.setRecords(list);
    }

    @Override
    public WorkFlowDefault getActiveByFlowType(Integer flow_type){
        List<WorkFlowDefault> list = list(new QueryWrapper<WorkFlowDefault>().eq("flow_type",flow_type)
        .isNull("deleted_at").eq("status",1));
        if(CollectionUtils.isEmpty(list)) throw new CommonException("流程数据错误，不存在启用的对应审批流程");
        if(list.size()>1) throw new CommonException("流程数据错误，存在两个启用中的同类型流程");
        return list.get(0);
    }

    @Override
    public JSONObject getTypeNameAndRouter() {
        JSONObject result = new JSONObject();
        List<WorkFlowTypeNameAndRouterVO> vos = baseMapper.getTypeNameAndRouter();
        if(CollectionUtils.isNotEmpty(vos)){
            for(WorkFlowTypeNameAndRouterVO vo: vos){
                JSONObject inner = new JSONObject();
                inner.put("name", vo.getName());
                inner.put("router", vo.getRouter());
                result.put(vo.getFlow_type().toString(), inner);
            }
        }
        return result;
    }
}
